﻿using Engine.UI;
using Message;
using UnityEngine;
using UnityEngine.UI;

namespace Login
{
    /// <summary>
    /// 登录面板
    /// </summary>
    public class LoginView : ViewBase
    {
        private InputField uname;
        private InputField pwd;

        private Button loginBtn;
        private Button backBtn;

        public override string GetPath()
        {
            return RLogin.LoginRoot;
        }

        public override void OnLoaded(Transform prefab)
        {
            base.OnLoaded(prefab);
            loginBtn = prefab.Find("loginBtn").GetComponent<Button>();
            backBtn = prefab.Find("backBtn").GetComponent<Button>();
            uname = prefab.Find("uname").GetComponent<InputField>();
            pwd = prefab.Find("pwd").GetComponent<InputField>();

            loginBtn.onClick.AddListener(OnLogin);
            backBtn.onClick.AddListener(OnBack);

            NetManager.Instance.OnConnect += OnConnect;
        }

        private void OnLogin()
        {
            //连接服务器
            if (NetManager.Instance.state != State.Connected)
            {
                NetManager.Instance.Connect();
                return;
            }
            else
            {
                Login();
            }
        }

        void OnConnect(State status)
        {
            Log.Debug("status:" + status.ToString());
            if (status == State.Connected)
            {
                Login();
            }
            if (status == State.TimeOut)
            {
                UIManager.Instance.Show<TipView>().SetTip("连接服务器超时!");
            }
            if (status == State.Error)
            {
                UIManager.Instance.Show<TipView>().SetTip("连接服务器出错!");
            }
        }

        private void OnBack()
        {
            Close();
            UIManager.Instance.Show<RegisterView>();
        }

        void Login()
        {
            //注册登录返回结果回调
            MsgManager.Instance.AddListener(typeof(S2CLoginRet), OnLoginBack);

            //发送
            C2SLogin protocol = new C2SLogin();
            protocol.uname = uname.text;
            protocol.pwd = Helper.GetStringHash(pwd.text);
            NetManager.Instance.Send(protocol);
        }

        private void OnLoginBack(object obj)
        {
            Log.Debug("OnLoginBack");
            //移除登录返回结果回调
            MsgManager.Instance.RemoveListener(typeof(S2CLoginRet), OnLoginBack);

            S2CLoginRet stcl = obj as S2CLoginRet;
            if (stcl.code == 0)
            {
                Log.Debug("uid:" + stcl.uid);
                GlobeData.uid = stcl.uid;
                GlobeData.uname = uname.text;
                UIManager.Instance.Show<TipView>().SetTip("登录成功!");
                //PanelMgr.Instance.OpenPanel<MainPanel>("MainPanel");
                UIManager.Instance.Show<RoleView>();
                Close();
            }
            else
            {
                UIManager.Instance.Show<TipView>().SetTip("登录失败!");
            }
        }

        public override void Close()
        {
            base.Close();
            NetManager.Instance.OnConnect -= OnConnect;
        }
    }
}